<?php
import( 'de.ceus-media.framework.krypton.core.MailSender' );
/**
 *	Sends Mails built from Templates.
 *	@package		mv2.logic
 *	@extends		Framework_Krypton_Core_MailSender
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			16.03.2007
 *	@version		0.2
 */
/**
 *	Sends Mails built from Templates.
 *	@package		mv2.logic
 *	@extends		Framework_Krypton_Core_MailSender
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			16.03.2007
 *	@version		0.2
 */
class Logic_TemplateUserMailSender
{
	/**	@var	Logic_User			$logic			Instance of User Logic */
	protected $logic		= null;
	/**	@var	Core_MailSender		$sender			Instance of Mail Sender */		
	protected $sender		= null;
	/**	@var	string				$mailPath		Encoded Path to Mail Templates */		
	protected $mailPath		= "";
	/**	@var	string				$subjectPath	Encoded Path to Subject Templates */		
	protected $subjectPath	= "";
	/**	@var	string				$languagePath	Encodes Language Path within Template Paths */
	protected $languagePath	= "";
	/**	@var	array				$receiverData	Array of Receiver Data for Template */
	protected $receiverData	= array();
	
	/**
	 *	Constructur, sets inner Language to current outer Language.
	 *	@access		public
	 *	@param		string		$mailPath		Encoded Path to Mail Templates
	 *	@param		string		$subjectPath	Encoded Path to Subject Templates
	 *	@return		void
	 */
	public function __construct( $mailPath = "mails.", $subjectPath = "mailsubjects." )
	{
		$this->setMailPath( $mailPath );
		$this->setSubjectPath( $subjectPath );
		$this->setLanguagePath( Framework_Krypton_Core_Registry::getStatic( 'language' )->getLanguage() );
		$this->setUp();
		$this->logic	= new Logic_User();
	}
	
	/**
	 *	Loads a Template for several Data to be set in. Does not set Body of Mail.
	 *	@access		public
	 *	@param		string		$filename		Filename of Template
	 *	@return		void
	 */
	public function send( $test = false )
	{
		$result	= $this->sender->send( $test );
		$this->setUp();
		return $result;
	}	
	
	/**
	 *	Sets Receiver of a Copy of all Mails.
	 *	@access		public
	 *	@param		int		$receiver		Receiver Address of Mail Copy
	 */
	public function setCopyReceiver( $receiver )
	{
		$this->sender->addHeader( "Bcc", $receiver );
	}
	
	/**
	 *	Sets Language of Templates.
	 *	@access		public
	 *	@param		string		$path			Encodes Language Path within Template Paths
	 *	@param		bool		$toLower		Transform to lower
	 *	@return		void
	 */
	public function setLanguagePath( $path, $toLower = true )
	{
		if( $path && !preg_match( "@\.$@", $path ) )
			$path	.= ".";
		if( $toLower )
			$path	= strtolower( $path );
		$this->languagePath	= $path;
	} 
	
	/**
	 *	Sets Path of Mail Templates.
	 *	@access		public
	 *	@param		string		$path			Encoded Path to Mail Templates
	 *	@param		bool		$toLower		Transform to lower
	 *	@return		void
	 */
	public function setMailPath( $path, $toLower = true )
	{
		if( $path && !preg_match( "@\.$@", $path ) )
			$path	.= ".";
		if( $toLower )
			$path	= strtolower( $path );
		$this->mailPath	= $path;
	} 
	
	/**
	 *	Sets User Data and User Language for Templates. Works only if setTemplate has been used.
	 *	@access		public
	 *	@param		int		$userID		User ID of Receiver
	 */
	public function setReceiverID( $userID )
	{
		$data	= $this->logic->getUserData( $userID );
		unset( $data['password'] );
		$this->sender->setReceiver( $data['email'] );
		if( $data['language'] )
			$this->setLanguagePath( $data['language'] );
		foreach( $data as $key => $value )
		{
			$data['receiver.'.$key]	= $value;
			unset( $data[$key] );
		}
		$this->receiverData	= $data;	
	}
	
	/**
	 *	Sets Address of Mail Sender.
	 *	@access		public
	 *	@param		string		$sender			Sender Address of Mail
	 *	@return		void
	 */
	public function setSender( $sender )
	{
		$this->sender->setSender( $sender );
	}
	
	/**
	 *	Sets up Mail Sender and clears Receiver Data.
	 *	@access		public
	 *	@return		void
	 */
	protected function setUp()
	{
		$this->sender	= new Framework_Krypton_Core_MailSender();
		$this->sender->addHeader( "Content-type", "text/html; charset=iso-8859-1" );
		$this->receiverData	= array();
	}
	
	/**
	 *	Loads a Subject Template, sets Data Elements, creates and sets Mail Subject.
	 *	@access		public
	 *	@param		string		$fileName		Filename of Template
	 *	@param		array		$data			Associative Array of Data to set in
	 *	@return		void
	 */
	public function setSubject( $fileName, $data )
	{
		$uri		= $this->subjectPath.$this->languagePath.$fileName;
		$template	= new View_Component_Template( $uri, $data );
		$this->sender->setSubject( $template->create() );
	}
	
	/**
	 *	Sets Path of Mail Templates.
	 *	@access		public
	 *	@param		string		$path			Encoded Path to Subject Templates
	 *	@param		bool		$toLower		Transform to lower
	 *	@return		void
	 */
	public function setSubjectPath( $path, $toLower = true )
	{
		if( $path && !preg_match( "@\.$@", $path ) )
			$path	.= ".";
		if( $toLower )
			$path	= strtolower( $path );
		$this->subjectPath	= $path;
	} 
	
	/**
	 *	Loads a Mail Template, sets Data Elements, creates and sets Mail Body. The Receiver ID must be set before.
	 *	@access		public
	 *	@param		string		$fileName		Filename of Template
	 *	@param		array		$data			Associative Array of Data to set in
	 *	@return		void
	 */
	public function setTemplate( $fileName, $data )
	{
		if( !$this->receiverData )
			throw new Framework_Krypton_Exception_Logic( "No Receiver ID set yet." );
		$uri		= $this->mailPath.$this->languagePath.$fileName;
		$data		= array_merge( $data, $this->receiverData );
		$template	= new View_Component_Template( $uri, $data );
		$this->sender->setBody( $template->create() );
	}
	
	/**
	 *	Public Method for PHPUnit-Tests of protected or private Methods.
	 *	@access		public
	 *	@param		string		$method			Name of Method
	 *	@param		mixed		$argument1		Argument 1 of Method (optional)	
	 *	@param		mixed		$argument1		Argument 2 of Method (optional)	
	 *	@param		mixed		$argument1		Argument 3 of Method (optional)	
	 *	@return		mixed
	 */
	public function testMethod( $method, $argument1 = null, $argument2 = null, $argument3 = null )
	{
		return $this->$method( $argument1, $argument2, $argument3 );
	}
	
	/**
	 *	Public Method for PHPUnit-Tests to get protected or private Members Variables.
	 *	@access		public
	 *	@param		string		$member			Name of Member Variable
	 *	@return		mixed
	 */
	public function testGet( $member )
	{
		return $this->$member;
	}
}
?>